package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Departments;
import com.itgaoshu.hospitalproject.bean.Doctor;
import com.itgaoshu.hospitalproject.bean.Paiban;
import com.itgaoshu.hospitalproject.bean.Registeredtype;
import com.itgaoshu.hospitalproject.service.DoctorService;
import com.itgaoshu.hospitalproject.service.PaibanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("doctor")
@Controller
public class DoctorController {
    @Autowired
    private DoctorService doctorService;




    //查询医生列表的方法
    @RequestMapping("doctorList")
    @ResponseBody
    public Object doctorList(Doctor doctor,Integer limit,Integer page){
        PageHelper.startPage(page,limit);
        List<Doctor> doctors = doctorService.doctorList(doctor);
        PageInfo pageInfo = new PageInfo(doctors);
        Map<String,Object> tableData = new HashMap<>();
        //这是layui要求返回的json数据类型
        tableData.put("code",0);
        tableData.put("msg","");
        //将全部数据的条数作为count传给前台
        tableData.put("count",pageInfo.getTotal());
        //将分页后的数据返回
        tableData.put("data",pageInfo.getList());
        return tableData;
    }

    //删除用户的方法
    @RequestMapping("deleteDoctor")
    @ResponseBody
    public Object deleteDoctor(int doctorId){
        // int i = doctorService.count(doctorId);
      // if(i>0){
          // return "这个医生还有病人哦";
       //}else{
           int result = doctorService.deleteDoctor(doctorId);
           if(result>0){

                return "删除成功";
           }else{
               return "删除失败";
           }
       }



    //查询所有科室的方法
    @RequestMapping("findAllDepartments")
    @ResponseBody
    public Object findAllDepartments(){
        List<Departments> allDepartments = doctorService.findAllDepartments();
        return allDepartments;
    }

    //查询所有挂号类型的方法
    @RequestMapping("findAllRegisteredtype")
    @ResponseBody
    public Object findAllRegisteredtype(){
        List<Registeredtype> allRegisteredtype = doctorService.findAllRegisteredtype();
        return allRegisteredtype;
    }

    @Autowired
    private PaibanService paibanService;

    //添加医生的方法
    @RequestMapping("addDoctor")
    @ResponseBody
    public Object addDoctor(Doctor doctor,Integer departmentId,Integer registeredId){
        doctor.setDepartmentId(departmentId);
        doctor.setRegisteredId(registeredId);
        int result = doctorService.addDoctor(doctor);
        if(result>0){
           return "添加成功";
        }else{
            return "添加失败";
        }
    }


    //修改医生的方法
    @ResponseBody
    @RequestMapping("editDoctor")
    public  Object editDoctor(Doctor doctor){

        int result = doctorService.editDoctor(doctor);
        if(result>0){
            return "修改成功";
        }else{
            return "修改失败";
        }

    }


}













